package com.gw.yunke.service.impl;

import com.gw.yunke.config.PropertyConfig;
import com.gw.yunke.dao.CourseMapper;
import com.gw.yunke.dao.ImageMapper;
import com.gw.yunke.dao.OtherFileMapper;
import com.gw.yunke.domain.Course;
import com.gw.yunke.domain.Image;
import com.gw.yunke.domain.OtherFile;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.Video;
import com.gw.yunke.domain.dto.CourseModifyReq;
import com.gw.yunke.service.IImageService;
import com.gw.yunke.service.IVideoService;
import com.gw.yunke.utils.VideoUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.crypto.Data;

@Service
public class MultipartFileService {

  @Autowired
  private IVideoService videoService;
  @Autowired
  private ImageMapper imageMapper;
  @Autowired
  private PropertyConfig propertyConfig;
  @Autowired
  private CourseMapper courseMapper;
  @Autowired
  private OtherFileMapper otherFileMapper;

  public Long upLoadVideo(MultipartFile file, User user,Long courseId) throws IOException, ParseException {

    String path = propertyConfig.getVideo_directory_path();
    File videoFile = new File(path, file.getOriginalFilename());

    file.transferTo(videoFile);

    Video video = new Video();
    video.setName(file.getOriginalFilename());
    video.setCreaterId(user.getId());
    video.setLocalUrl(videoFile.getAbsolutePath());
    video.setRequestUrl("" + System.currentTimeMillis());
    video.setCreateTime(new Date());
    DateFormat format = new SimpleDateFormat("HH:mm:ss");
    //String length = VideoUtil.getVideoTime(videoFile.getAbsolutePath());
    String length = "01:32:10.88";
    video.setLength(format.parse(length.substring(0, length.indexOf('.') - 1)));
    video.setSize((int) (file.getSize() / (1024 * 1024)));
    int result = videoService.createVideo(video);
    Date data = courseMapper.selectByPrimaryKey(courseId).getAllTime();
    //TODO: 加时间
    if(result > 0)
      return video.getId();
    return -1L;
  }

  public Long upLoadVideo(MultipartFile file, User user) throws IOException, ParseException {

    String path = propertyConfig.getVideo_directory_path();
    File videoFile = new File(path, file.getOriginalFilename());

    file.transferTo(videoFile);

    Video video = new Video();
    video.setName(file.getOriginalFilename());
    video.setCreaterId(user.getId());
    video.setLocalUrl(videoFile.getAbsolutePath());
    video.setRequestUrl("" + System.currentTimeMillis());
    video.setCreateTime(new Date());
    DateFormat format = new SimpleDateFormat("HH:mm:ss");
    String length = VideoUtil.getVideoTime(videoFile.getAbsolutePath());
    //String length = "01:32:10.88";
    video.setLength(format.parse(length.substring(0, length.indexOf('.') - 1)));
    video.setSize((int) (file.getSize() / (1024 * 1024)));
    int result = videoService.createVideo(video);

    if(result > 0)
      return video.getId();
    return -1L;
  }

  public int upLoadPPT(MultipartFile file,Long courseId, User user) throws IOException, ParseException {

    String path = propertyConfig.getOther_directory_path();
    File pptFile = new File(path, file.getOriginalFilename());

    file.transferTo(pptFile);

    OtherFile otherFile = new OtherFile();
    otherFile.setType((byte)0);
    otherFile.setCourseId(courseId);
    otherFile.setLocalUrl(pptFile.getAbsolutePath());
    otherFile.setSize((int)(file.getSize()/(1024*1024)));
    otherFile.setName(file.getOriginalFilename());

    int result = otherFileMapper.insertSelective(otherFile);

    if(result > 0)
      return 1;
    return 0;
  }


  public Long upLoadImage(MultipartFile file) throws IOException, ParseException {

    String path = propertyConfig.getImage_directory_path();
    File imageFile = new File(path, file.getOriginalFilename());

    file.transferTo(imageFile);

    Image image = new Image();
    image.setLocalUrl(imageFile.getAbsolutePath());
    int result = imageMapper.insertSelective(image);
    if (result > 0) {
      return image.getId();
    } else {
      return -1L;
    }


  }



}
